package com.jy.horny.function.info

import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jy.chat.kit.model.ActionModel
import com.jy.horny.ToastUtil
import com.jy.horny.adapter.ProfilePhotoAdapter
import com.jy.horny.base.BaseVMActivity
import com.jy.horny.base.HttpRequestListener
import com.jy.horny.databinding.ActivityProfileBinding
import com.jy.horny.dialog.UserActionPopup
import com.jy.horny.model.HomeListModel
import com.jy.horny.model.UserInfoModel
import com.jy.horny.utils.loadThumbnails
import com.lib.module_base.utils.StatusBarUtil


class ProfileActivity : BaseVMActivity<ActivityProfileBinding, ProfileViewModel>(
    ActivityProfileBinding::inflate,
    ProfileViewModel::class.java
) {
    override fun initData() {
        initialViews()
    }
    private fun initialViews() {
        val extras = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            intent.extras?.getSerializable("model",HomeListModel.MatchIndexBean::class.java)
        }else{
            intent.extras?.getSerializable("model") as HomeListModel.MatchIndexBean
        }
        binding.titleBar.setBackListener { finish() }
        binding.titleBar.setPadding(0,StatusBarUtil.getStatusBarHeight(this),0,0)
        binding.titleBar.setMoreVisible(true)
        val targetId = extras?.friendUserCode ?: intent.getStringExtra("userCodeFriend")?:""
        binding.titleBar.setMoreListener {
            UserActionPopup(this, targetId,object :UserActionPopup.UserActionListener{
                override fun userReport(reason: String) {
                    addAction(4,reason, targetId)
                }

                override fun userBlack() {
                    addAction(1,null, targetId)

                }

            }).showPopupWindow()
        }
        if (extras==null){
            mViewModel?.getUserInfo(object :HttpRequestListener<UserInfoModel>(){
                override fun addRequestParams(params: HashMap<String, Any>) {
                    params["userCode"]=intent.getStringExtra("userCodeFriend")?:""
                }
                override fun success(t: UserInfoModel?) {
                    t?.apply {
                        binding.imgUserBg.loadThumbnails(t.avatarUrl,binding.imgUserBg.width)
                        binding.txtProfileName.text="${t.nickName}\n${t.age}"
                        binding.profilePhotoList.layoutManager=LinearLayoutManager(this@ProfileActivity,RecyclerView.HORIZONTAL,false)
                        val profilePhotoAdapter = ProfilePhotoAdapter()
                        profilePhotoAdapter.addAll(t.imagesV2)
                        binding.profilePhotoList.adapter= profilePhotoAdapter
                    }

                }

            })
        }
        extras?.let {
            binding.imgUserBg.loadThumbnails(extras.avatarUrl,binding.imgUserBg.width)
            binding.txtProfileName.text="${extras.nickName}\n${extras.age}"
            binding.profilePhotoList.layoutManager=LinearLayoutManager(this,RecyclerView.HORIZONTAL,false)
            val profilePhotoAdapter = ProfilePhotoAdapter()
            profilePhotoAdapter.addAll(extras.images)
            binding.profilePhotoList.adapter= profilePhotoAdapter

        }


    }

    fun addAction(opType:Int,reason:String?,userCodeFriend:String){
        mViewModel?.addAction(object : HttpRequestListener<ActionModel>() {
            override fun addRequestParams(params: HashMap<String, Any>) {
                params["opType"] = opType
                reason?.apply {
                    params["reason"] = reason
                }
                params["userCodeFriend"] = userCodeFriend
            }

            override fun success(t: ActionModel?) {
                if (opType==4){
                    ToastUtil.showToast(getString(com.lib.module_res.R.string.report_toast))
                }else{
                    ToastUtil.showToast(getString(com.lib.module_res.R.string.black_toast))
                }
            }

        })
    }

    companion object {
        fun start(context: Context?, model: HomeListModel.MatchIndexBean?) {
            val intent = Intent(context, ProfileActivity::class.java)
            intent.putExtra("model",model)
            context?.startActivity(intent)
        }
    }

}